(function (angular) {
    'use strict';

    angular.module('ytong.services').factory('authenticationService',authenticationService);

    function authenticationService(proxyService, Constants,$log, $q, growl, $state){

        var currentUser = undefined;

        function login(username, password){

            var defer = $q.defer();

            proxyService.get(Constants.URLS.LOGIN_URL, {params: {username: username, password: password}})
                .then(function(responseData){
                    currentUser = responseData.user;
                    $log.info("Login successful.");
                    
                    if(currentUser.role === '0'){
                        defer.resolve(Constants.STATES.ADMIN.DASHBOARD);
                    }else if(currentUser.role === '1'){
                        defer.resolve(Constants.STATES.MANAGER.DASHBOARD);
                    }else if(currentUser.role === '2'){
                        defer.resolve(Constants.STATES.SALESMAN.DASHBOARD);
                    }else if(currentUser.role === '-1'){
                        growl.error("User not authorized yet");
                        defer.reject();
                    }

                },function(responseData){
                    if(responseData == null){
                        growl.error("Error connecting to server");
                        defer.reject();
                    }else{
                        growl.error("Login failed: " + responseData.error)
                        defer.reject();
                    }
                });

            return defer.promise;
        };

        function logout(){
            currentUser = undefined;
            $state.go(Constants.STATES.LOGIN);
        };

        function register(user){

            var defer = $q.defer();

            proxyService.post(Constants.URLS.LOGIN_URL, {user: user})
                .then(function(responseData){
                    currentUser = responseData.user;
                    $log.info("User " + user.username + " registration successful.");
                    growl.success("Registration successful");
                    defer.resolve();

                }, function(){
                    growl.error("Registration failed");
                    defer.reject();
                });

            return defer.promise;
        };

        function getCurrentUserId(){
            return currentUser.id;
        };

        function getCurrentUserRole(){
            return currentUser.role;
        };

        function validateUserRole(role){
            return currentUser.role === role;
        };

        //interface
        return{
            login: login,
            logout: logout,
            register: register,
            getCurrentUserID: getCurrentUserId,
            getCurrentUserRole: getCurrentUserRole,
            validateUserRole: validateUserRole
        }
    };

}(angular));